<?php


namespace App\Models\Pay;


use App\Models\Jd;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\UserRecharge;
use App\User;

class Notify
{
    public static function notifyOrder($no, $notifyResult = '')
    {
        $order = Order::where('no', $no)->first();
        if (!$order) {
            uexception('订单不存在');
        }
        if ($order['is_pay'] == 1) {
            uexception('订单已支付');
        }
        if ($order['pay_type'] == Order::PAY_TYPE[1]) {
            $user = User::find($order['user_id']);
            if (empty($user)) {
                uexception('用户不存在');
            }
            $params = [
                'change' => $order['total_amount'],
                'type' => 2,
                'desc' => '购买商品',
            ];
            User::moneyChange($params, $user);
        }
        $order->is_pay = 1;
        $order->order_status = Order::ORDER_STATUS[1];
        $order->order_status_desc = Order::ORDER_STATUS_DESC[1];
        $order->paid_at = date('Y-m-d H:i:s');
        $row = $order->save();
        if ($row) {
            OrderItem::orderStock($order['id']);  //减库存 加销量
            //通知收货人
        }
        Jd::pushJdOrder($order);
        return $row;
    }
}